package LeetCode.month02.day37;

import java.util.Arrays;
import java.util.Objects;

public class No151 {
    public static void main(String[] args) {
        System.out.println(reverseWords("  sa   dadf fda  dsfaf    dasfe"));
    }
    public static String reverseWords(String s) {
        s = s.trim();
        int n = s.length();
        StringBuilder sb = new StringBuilder();
        String[] strings = s.split("\\s+");
        for (int i = strings.length - 1; i >= 0; i--) {
//            String s1 = strings[i].trim();
//            if(!s1.isEmpty()){
//                sb=sb.append(s1).append(" ");
//            }
            sb = sb.append(strings[i]+" ");
        }
        return sb.toString().trim();
    }
}
